
#if !defined PKG_SENDER_H  && !defined (_NEW_PROTO_TP)
#define PKG_SENDER_H

#include "RtCnBase.h"
#include "RtConnBase.h"

class CPkgSender;
typedef CConnConnectorT<CPkgSender>		CPkgSenderConnector;

typedef ServerListT<CPkgSender>			CPkgSenderServerList;
typedef CConnAcceptorSinkT<CPkgSender>	CPkgSenderAcceptorSink;
typedef CConnAcceptorT<CPkgSender>		CPkgSenderAcceptor;

class /*RT_API_EXPORT*/ CPkgSender : public CRtConnBase
{
public:
	virtual RtResult SendData(
		CRtMessageBlock& inData,
		BOOL inSendImmediately = TRUE,
		BOOL inDestroyData = FALSE,
		DataPriority inPriority = DP_MEDIUM, 
		BOOL inNeedAck = TRUE);

	virtual RtResult SetOption(
		DWORD aCommand, 
		LPVOID aArg);

	virtual RtResult GetOption(
		DWORD aCommand, 
		LPVOID aArg);

	virtual RtResult Disconnect(
		RtResult aReason);
public:
	// interface IRtAcceptorConnectorSink
	virtual void OnConnectIndication(
		RtResult aReason,
		IRtTransport *aTrpt,
		IRtAcceptorConnectorId *aRequestId);

	virtual void OnReceive(
		CRtMessageBlock &aData,
		IRtTransport *aTrptId,
		CRtTransportParameter *aPara = NULL);

	virtual void OnSend(
		IRtTransport *aTrptId,
		CRtTransportParameter *aPara = NULL);

	virtual void OnDisconnect(
		RtResult aReason,
		IRtTransport *aTrptId);

	virtual void OnTimer(CRtTimerWrapper* aId);

public:
	//Unconnected time stamp
	CRtTimeValue GetDisconnTimestamp();
	void TryOnDisconnIndicate();
protected:
	void Reset();
	RtResult SendData_i();
public:
	CPkgSender(CRtConnectionManager::CType);
	virtual ~CPkgSender();

	void NeedKeepAlive(BOOL bNeedKeepAlive);
	void SetConnConnector(CPkgSenderConnector* pConnConnector);
	//Cancel connect call, stop the handshake if connection hasn't made
	void CancelHandShake();
	void ScheduleConnectTimer(const CRtTimeValue& aTimeout) {}
	
	//In order to keep one instance of Server List.
	void SetServerList(CPkgSenderServerList* pSvrList);
	void SetConnAcceptor(CPkgSenderAcceptor* pConnAcceptor);
protected:
	CRtMessageBlock *m_pmbLocSendData;	//local Data block for Send

	CPkgSenderConnector* m_pConnConnector;		//Client use

	CPkgSenderServerList* m_pSvrList;							//Server use
	CRtAutoPtr<CPkgSenderAcceptor> m_pConnAcceptor;
	CRtConnectionManager::CType m_cType;

	CRtTimeValue m_disconn_timestamp;
};

#endif	//PKG_SENDER_H
